import "./style.scss";
import hljs from "./hljs";
import LightBox from "./lightBox";

/** 代码块 */
class CodeBlock {
	/** 文章dom元素 */
	private article = document.querySelector(".markdown-body");
	/** 复制按钮定时器 */
	private copyBtnTimers = new Map<Element, NodeJS.Timeout>();

	constructor() {
		if (!this.article) return;
		this.init();
	}

	/** 初始化 */
	private init () {
		const codeBlockArr = Array.from(this.article!.querySelectorAll("pre"));
		codeBlockArr.forEach((codeBlock) => {
			const codeDom = codeBlock.firstChild as HTMLElement;
			// 代码高亮
			this.highlight(codeDom);
		});
	}

	/** 代码高亮 */
	private highlight (codeDom: HTMLElement) {
		hljs.highlightElement(codeDom);
		// 将cpp转化为c++
		if (codeDom.classList.contains("language-cpp")) {
			codeDom.classList.remove("language-cpp");
			codeDom.classList.add("language-c++");
		}
	}

	/** 创建code-header模板 */
	private createCodeHeaderTemplate () {
		const wrapper = document.createElement("div");
		wrapper.classList.add("code-block-header");
	}

	/** 创建折叠按钮 */
	private createFoldBtn () {

	}

	/** 折叠按钮点击事件 */
	private foldBtnClick = (event: Event) => {

	};

	/** 创建代码语言文本 */
	private createLanguageText (codeDom: HTMLElement) {

	}

	/** 获取code元素上的语言 */
	private getCodeLanguage (codeDom: HTMLElement) {

	}

	/** 创建复制按钮 */
	private createCopyBtn () {

	}

	/** 复制按钮点击事件 */
	private copyBtnClick = (event: Event) => {

	};

	/** 添加行号 */
	private addLineNumber (codeDom: HTMLElement) {

	}
}

new CodeBlock();
/** 灯箱处理 */
const markdownBody = document.querySelector(".markdown-body");
const imgEleList: HTMLElement[] = Array.from(document.querySelectorAll(".markdown-body img"));
imgEleList.forEach((ele: HTMLElement) => {
	ele.parentNode && (ele.parentNode as HTMLElement).replaceWith(ele);
});
if (markdownBody) new LightBox(markdownBody as HTMLElement);
